#!/bin/bash
alias mv='mv' # 取消强制覆盖时的确认
alias echo='echo -e'

if [ ! -f /home/${USER}/0_source_env.sh ]; then
    echo "/home/${USER}/0_source_env.sh is not exist"
    exit 1
fi
source /home/${USER}/0_source_env.sh
LOGFILE="/tmp/rename_quotalog"

SRCQUOTA="/${QUOTA_TEST}/srcQuotaDir"
Q_SRCDIR="${FS_MOUNTPOINT}${SRCQUOTA}" # Q_ 开头的目录代表创建了配额

DESTQUOTA="/${QUOTA_TEST}/destQuotaDir"
Q_DESTDIR="${FS_MOUNTPOINT}${DESTQUOTA}"

NOQUOTADIR="${FS_MOUNTPOINT}/${QUOTA_TEST}/noQuotaDir"
Q_CHILDDIR="${NOQUOTADIR}/childQuotaDir"
CHILDQUOTA="${QUOTA_TEST}/noQuotaDir/childQuotaDir"

USER="u1"; GROUP="u1"
Q_UID=`cat /etc/passwd | grep ${USER} | awk -F: '{print $3}'`
Q_GID=`cat /etc/passwd | grep ${USER} | awk -F: '{print $4}'`

if (($1 == 1)) || (($1 == 0));then
    echo "\n==1===目标目录不能存在, vfs会报错"
    cmd="mkdir -p ${Q_SRCDIR}/dir_exist";echo "\t${cmd}"; eval ${cmd}
    cmd="mkdir -p ${Q_DESTDIR}/dir_exist";echo "\t${cmd}"; eval ${cmd}
    cmd="mv ${Q_SRCDIR}/dir_exist ${Q_DESTDIR}";echo "\t${cmd}"; eval ${cmd} &>${LOGFILE}
    msg=`cat ${LOGFILE}`; echo "\t$msg"; grep "File exists" ${LOGFILE} &>/dev/null;
    if (($? == 0)); then echo "\t验证成功"; else echo "\t验证失败"; exit 1; fi
fi

if (($1 == 2)) || (($1 == 0));then
    echo "\n==2===不能 rename/mv 配额目录本身"
    cmd="mv $Q_SRCDIR $Q_DESTDIR";echo "\t${cmd}"; eval ${cmd} &>${LOGFILE}
    msg=`cat ${LOGFILE}`; echo "\t$msg"; grep "Device or resource busy" ${LOGFILE} &>/dev/null;
    if (($? == 0)); then echo "\t验证成功"; else echo "\t验证失败"; exit 1; fi
fi

if (($1 == 3)) || (($1 == 0));then
    echo "\n==3===不能 rename/mv 配额目录的其父目录"
    cmd="mv ${NOQUOTADIR} ${Q_DESTDIR}";echo "\t${cmd}"; eval ${cmd} &>${LOGFILE}
    msg=`cat ${LOGFILE}`; echo "\t$msg"; grep "Device or resource busy" ${LOGFILE} &>/dev/null;
    if (($? == 0)); then echo "\t验证成功"; else echo "\t验证失败"; exit 1; fi
fi

if (($1 == 4)) || (($1 == 0));then
    echo "\n==4===源目录和目标目录都不属于配额目录"
    mkdir -p ${NOQUOTADIR}/src
    cmd="mv ${NOQUOTADIR}/src ${NOQUOTADIR}/dest";echo "\t${cmd}"; eval ${cmd}
    if (($? == 0)); then echo "\t验证成功"; else echo "\t验证失败"; exit 1; fi
fi

if (($1 == 5)) || (($1 == 0));then
    echo "\n==5===源目录和目标目录属于同一配额目录(配额使用量不变)"
    mkdir -p ${Q_SRCDIR}/srcdir
    dd if=/dev/zero of=${Q_SRCDIR}/srcdir/file1 bs=3M count=1 &> /dev/null
    dd if=/dev/zero of=${Q_SRCDIR}/srcdir/file2 bs=3M count=1 &> /dev/null

    old_used=`orcafs-ctl --listquota | grep -E -w "${SRCQUOTA}" | awk '{print $9}'`
    echo -n -e "before mv(${old_used}):\n\t [ls ${Q_SRCDIR}]: "; ls ${Q_SRCDIR}

    echo -n -e "exec mv cmd:\n";
    cmd="mv ${Q_SRCDIR}/srcdir ${Q_SRCDIR}/destdir";echo "\t${cmd}"; eval ${cmd}

    new_used=`orcafs-ctl --listquota | grep -E -w "${SRCQUOTA}" | awk '{print $9}'`
    echo -n -e "after mv(${new_used}):\n\t [ls ${Q_SRCDIR}]: "; ls ${Q_SRCDIR}

    if ((${old_used} == ${new_used})); then echo "验证成功"; else echo "验证失败"; exit 1; fi
fi

if (($1 == 6)) || (($1 == 0));then
    echo "\n==6===源目录和目标目录都被设置配额, 但分别属于两个不同的配额目录, 验证目标配额未超出"
    rm -rf ${Q_SRCDIR}/*
    mkdir -p ${Q_SRCDIR}/srcdir
    dd if=/dev/zero of=${Q_SRCDIR}/srcdir/file1 bs=2M count=1 &> /dev/null

    src_old_used=`orcafs-ctl --listquota | grep -E -w "${SRCQUOTA}" | awk '{print $9}'`
    dest_old_used=`orcafs-ctl --listquota | grep -E -w "${DESTQUOTA}" | awk '{print $9}'`
    echo -n -e "before mv(${src_old_used}):\n\t [ls ${Q_SRCDIR}]: "; ls -a ${Q_SRCDIR}
    echo -n -e "before mv(${dest_old_used}):\n\t [ls ${Q_DESTDIR}]: "; ls -a ${Q_DESTDIR}

    #exit
    echo -n -e "exec mv cmd:\n";
    cmd="mv ${Q_SRCDIR}/srcdir ${Q_DESTDIR}/destdir";echo "\t${cmd}"; eval ${cmd}

    src_new_used=`orcafs-ctl --listquota | grep -E -w "${SRCQUOTA}" | awk '{print $9}'`
    dest_new_used=`orcafs-ctl --listquota | grep -E -w "${DESTQUOTA}" | awk '{print $9}'`
    echo -n -e "after mv(${src_new_used}):\n\t [ls ${Q_SRCDIR}]: "; ls -a ${Q_SRCDIR}
    echo -n -e "after mv(${dest_new_used}):\n\t [ls ${Q_DESTDIR}]: "; ls -a ${Q_DESTDIR}

    if ((${src_old_used} == ${dest_new_used})) && ((${src_new_used} == ${dest_old_used}));then
    echo "验证成功"; else echo "验证失败"; exit 1; fi
fi

if (($1 == 7)) || (($1 == 0));then
    echo "\n==7===源目录和目标目录都被设置配额, 但分别属于两个不同的配额目录, 验证目标配额超出"
    mkdir -p ${Q_SRCDIR}/srcdir
    dd if=/dev/zero of=${Q_SRCDIR}/srcdir/file1 bs=5M count=1 &> /dev/null
    dd if=/dev/zero of=${Q_SRCDIR}/srcdir/file2 bs=4M count=1 &> /dev/null

    src_old_used=`orcafs-ctl --listquota | grep -E -w "${SRCQUOTA}" | awk '{print $9}'`
    dest_old_used=`orcafs-ctl --listquota | grep -E -w "${DESTQUOTA}" | awk '{print $9}'`
    echo -n -e "before mv(${src_old_used}):\n\t [ls ${Q_SRCDIR}]: "; ls -a ${Q_SRCDIR}
    echo -n -e "before mv(${dest_old_used}):\n\t [ls ${Q_DESTDIR}]: "; ls -a ${Q_DESTDIR}

    echo -n -e "exec mv cmd:\n";
    cmd="mv ${Q_SRCDIR}/srcdir ${Q_DESTDIR}/destdir";echo -n -e "\t${cmd}\n\t"; eval ${cmd}
    if (($? == 1)); then echo "\tDisk quota exceeded, 测试成功"; break; else echo "验证失败"; exit 1;fi

    src_new_used=`orcafs-ctl --listquota | grep -E -w "${SRCQUOTA}" | awk '{print $9}'`
    dest_new_used=`orcafs-ctl --listquota | grep -E -w "${DESTQUOTA}" | awk '{print $9}'`
    echo -n -e "after mv(${src_new_used}):\n\t [ls ${Q_SRCDIR}]: "; ls -a ${Q_SRCDIR}
    echo -n -e "after mv(${dest_new_used}):\n\t [ls ${Q_DESTDIR}]: "; ls -a ${Q_DESTDIR}

    if ((${src_old_used} == ${src_new_used})) && ((${dest_new_used} == ${dest_old_used}));then
    echo "验证成功"; else echo "验证失败"; exit 1; fi
fi

if (($1 == 8)) || (($1 == 0));then
    echo "\n==8===源目录属于配额目录，目标目录不属于配额目录，配额不会超出"
    mkdir -p ${Q_SRCDIR}/srcdir
    dd if=/dev/zero of=${Q_SRCDIR}/srcdir/file1 bs=3M count=1 &> /dev/null
    dd if=/dev/zero of=${Q_SRCDIR}/srcdir/file2 bs=2M count=1 &> /dev/null

    src_old_used=`orcafs-ctl --listquota | grep -E -w "${SRCQUOTA}" | awk '{print $9}'`
    echo -n -e "before mv(${src_old_used}):\n\t [ls ${Q_SRCDIR}]: "; ls -a ${Q_SRCDIR}
    echo -n -e "\t [ls ${Q_DESTDIR}]: "; ls -a ${Q_DESTDIR}

    echo -n -e "exec mv cmd:\n";
    cmd="mv ${Q_SRCDIR}/srcdir ${NOQUOTADIR}/destdir";echo "\t${cmd}"; eval ${cmd}
    #if (($? == 1)); then echo "\tDisk quota exceeded, 测试成功"; break; else echo "验证失败"; exit 1;fi

    src_new_used=`orcafs-ctl --listquota | grep -E -w "${SRCQUOTA}" | awk '{print $9}'`
    echo -n -e "after mv(${src_new_used}):\n\t [ls ${Q_SRCDIR}]: "; ls -a ${Q_SRCDIR}
    echo -n -e "\t [ls ${Q_DESTDIR}]: "; ls -a ${Q_DESTDIR}

    if ((${src_old_used} != 0)) && ((${src_new_used} == 0));then
    echo "验证成功"; else echo "验证失败"; exit 1; fi
fi

if (($1 == 9)) || (($1 == 0));then
    echo "\n==9===源目录不属于配额目录，目标目录属于配额目录，验证目标配额未超出"
    mkdir -p ${NOQUOTADIR}/srcdir
    rm -rf ${Q_DESTDIR}/*
    dd if=/dev/zero of=${NOQUOTADIR}/srcdir/file1 bs=1M count=1 &> /dev/null
    dd if=/dev/zero of=${NOQUOTADIR}/srcdir/file2 bs=1M count=1 &> /dev/null

    dest_old_used=`orcafs-ctl --listquota | grep -E -w "${DESTQUOTA}" | awk '{print $9}'`
    echo -n -e "before mv(${dest_old_used}):\n\t [ls ${Q_DESTDIR}]: "; ls -a ${Q_DESTDIR}
    echo -n -e "\t [ls ${NOQUOTADIR}]: "; ls -a ${NOQUOTADIR}

    echo -n -e "exec mv cmd:\n";
    cmd="mv ${NOQUOTADIR}/srcdir ${Q_DESTDIR}/destdir";echo "\t${cmd}"; eval ${cmd}
    if (($? == 1)); then echo "\tDisk quota exceeded, 验证失败"; exit 1; fi

    dest_new_used=`orcafs-ctl --listquota | grep -E -w "${DESTQUOTA}" | awk '{print $9}'`
    echo -n -e "after mv(${dest_new_used}):\n\t [ls ${Q_DESTDIR}]: "; ls -a ${Q_DESTDIR}
    echo -n -e "\t [ls ${NOQUOTADIR}]: "; ls -a ${NOQUOTADIR}

    if ((${dest_new_used} != 0)) && ((${dest_old_used} == 0));then
    echo "验证成功"; else echo "验证失败"; exit 1; fi
fi

if (($1 == 10)) || (($1 == 0));then
    echo "\n==10===源目录不属于配额目录，目标目录属于配额目录，验证目标配额超出"
    mkdir -p ${NOQUOTADIR}/srcdir
    rm -rf ${Q_DESTDIR}/*
    dd if=/dev/zero of=${NOQUOTADIR}/srcdir/file1 bs=7M count=1 &> /dev/null
    dd if=/dev/zero of=${NOQUOTADIR}/srcdir/file2 bs=2M count=1 &> /dev/null

    dest_old_used=`orcafs-ctl --listquota | grep -E -w "${DESTQUOTA}" | awk '{print $9}'`
    echo -n -e "before mv(${dest_old_used}):\n\t [ls ${Q_DESTDIR}]: "; ls -a ${Q_DESTDIR}
    echo -n -e "\t [ls ${NOQUOTADIR}]: "; ls -a ${NOQUOTADIR}

    echo -n -e "exec mv cmd:\n";
    cmd="mv ${NOQUOTADIR}/srcdir ${Q_DESTDIR}/destdir";echo -n -e "\t${cmd}\n\t"; eval ${cmd}
    if (($? == 1)); then echo "\tDisk quota exceeded, 测试成功"; break; else echo "验证失败"; exit 1;fi

    dest_new_used=`orcafs-ctl --listquota | grep -E -w "${DESTQUOTA}" | awk '{print $9}'`
    echo -n -e "after mv(${dest_new_used}):\n\t [ls ${Q_DESTDIR}]: "; ls -a ${Q_DESTDIR}
    echo -n -e "\t [ls ${NOQUOTADIR}]: "; ls -a ${NOQUOTADIR}

    if ((${dest_new_used} == 0)) && ((${dest_old_used} == 0));then
    echo "验证成功"; else echo "验证失败"; exit 1; fi
fi

alias mv='mv -i' # 强制覆盖时需要确认
exit 0;


